/**
 * User: brianjeppesen
 * Date: 6/26/11
 * Time: 11:26 AM
 */

var st = "settings";
var aht = "article_header";
var aft = "article_footer";
var at = "author";
var bt = "book";
var pt = "photo";
var sjt = "subject";
var ndt = "namedrop";

var attribute_prefix = "";

/*
    Create custom tags to pretty up code within html templates.
 */
document.createElement(aht);
document.createElement(aft);
document.createElement(at);
document.createElement(bt);
document.createElement(pt);
document.createElement(st);
document.createElement(sjt);
document.createElement(ndt);

/*
    SETTINGS ==========================================
 */
function Settings(oNode) {
    /*
        Get attributes from author tag.
     */
    if (exists(oNode.attributes.amazon)) {
        this.amazon=oNode.attributes.amazon.value;
    }

    if (exists(oNode.attributes.bn)) {
        this.bn=oNode.attributes.bn.value;
    }

    if (exists(oNode.attributes.indiebound)) {
        this.indiebound=oNode.attributes.indiebound.value;
    }

    if (exists(oNode.attributes.powells)) {
        this.powells=oNode.attributes.powells.value;
    }

    this.li_am = function(id) {
        return ("<a href=\"http://www.amazon.com/gp/product/" + id +
            "/ref=as_li_tf_il?ie=UTF8&amp;tag=" + this.amazon + "&amp;linkCode=as2&amp;camp=217145&amp;creative=399349&amp;creativeASIN=" +
            id + "\" target=\"_blank\">Buy on Amazon</a>");
    }

    this.li_bn = function(id) {
        return ("<a href=\"http://search.barnesandnoble.com/booksearch/isbninquiry.asp?ean=" + id + "&cm_mmc=AFFILIATES-_-Linkshare-_-" + this.bn + "-_-10:1\"" +
            " target=\"_blank\">Buy on Barnes & Noble</a>");
    }

    this.li_in = function(id) {
        return ("<a href=\"http://www.indiebound.org/book/" + id + "?aff=" + this.indiebound + "\" target=\"_blank\">Buy on IndieBound</a>");
    }

    this.li_po = function(id) {
        return ("<a href=\"http://www.powells.com/partner/" + this.powells + "/biblio/" + id + "?p_cv\"  rel=\"powells-" +
            id + "\" target=\"_blank\">Buy on Powells</a>");
    }

    this.img_am = function(id) {
        return ("http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&Format=_SL160_&ASIN=" + id + "&ID=AsinImage&WS=1&ServiceVersion=20070822");
    }

    var self=this;
}



/*
    PAGE AREAS =======================================
 */

/*
    AJ:HEADER
 */
function AJHM(oHeader){
    this.__node = oHeader;                  // save the aj:header node

    /*
        Function: draw_all
        Description: calls all draw functions above.
     */
    this.d = function(){
        aj_authors.dby(this.__node);
        aj_subject.d(this.__node);
        aj_photos.dh(this.__node);
        aj_books.dbi(this.__node);
        aj_namedrops.dnd(this.__node);
    }

    var self=this;
}

/*
    AJ:FOOTER
 */
function AJFM(oFooter){
    this.__node = oFooter;                  // save the aj:footer node

    this.sln = function() {
        this.__node.innerHTML += "<center><b>We welcome letters to the editors, on this or any other subject, at <a href=\"mailto:letters@lareviewofbooks.org\" target=\"_blank\"><b>letters@lareviewofbooks.org.</b></a></b></center><br/>";
    }

    /*
        Function: draw_all
        Description: Calls all draw functions above.
     */
    this.d = function(){
        aj_authors.dbi(this.__node);
        aj_photos.dc(this.__node);
        this.sln();
        aj_books.dpb(this.__node);
    }

    var self=this;
}

/*
    AUTHOR STUFF =====================================
 */

/*
    Aj_Author
    Variable: oAuthor - the author node passed in by the manager
 */
function Aj_Author(oAuthor){
    /*
        Get attributes from author tag.
     */
    if (exists(oAuthor.attributes.name)) {
        this.__name=oAuthor.attributes.name.value;
    }

    if (exists(oAuthor.attributes.bio)) {
        this.__bio=oAuthor.attributes.bio.value;
    }

    /*
        Get functions
     */
    this.get_name = function() {
        return this.__name;
    }

    this.get_bio = function() {
        return this.__bio;
    }

    var self=this;
}

/*
    Aj_Author_Manager
    Manages all authors
 */
function Aj_Author_Manager(){
    this.__authors = new Array();   // Create storage for Aj_Author

    /*
        adds an author to storage
     */
    this.ad = function(oAuthor) {
        this.__authors.push(new Aj_Author((oAuthor)));
    }

    /*
        Draw bylines
     */
    this.dby = function(oNode) {
        this.output = document.createElement("div");    // create div
        this.output.className = "aj_byline";                       // div class = aj_byline

        // iterate authors
        for (var i=0; i<this.__authors.length; i++) {
            if(i>0) {                   // determine punctuation to be used
                if((this.__authors.length-i)<2) { this.output.innerHTML += " and "; }
                else { this.output.innerHTML += ", "; }
            }
            // get author name from Aj_Author
            this.output.innerHTML += this.__authors[i].get_name();
        }

        oNode.appendChild(this.output);     // add all this to the page.
    }

    /*
        Draw bios
     */
    this.dbi = function(oNode) {
        // iterate authors
        for (var i=0; i<this.__authors.length; i++) {
            // make sure author has a bio
            if (this.__authors[i].get_bio() != null && this.__authors[i].get_bio() !== "") {
                this.output = document.createElement("p");      // create a paragraph element
                this.output.className = "aj_bio";                           // p class = aj_bio
                this.output.innerHTML += this.__authors[i].get_bio();   // get the bio
                oNode.appendChild(this.output);     // add all this to the page.
            }
        }
    }

    var self=this;

}

/*
    IMAGE STUFF =====================================
 */

/*
    Aj_Photo
    Variable: oImage - the image node passed in by the manager
 */
function Aj_Photo(oPhoto){
    this.__node = oPhoto;
    /*
        Get attributes from author tag.
     */
    if (exists(oPhoto.attributes.src)) {
        this.__src=oPhoto.attributes.src.value;
    }

    if (exists(oPhoto.attributes.caption)) {
        this.__caption=oPhoto.attributes.caption.value;
    }

    if (exists(oPhoto.attributes.artist)) {
        this.__artist=oPhoto.attributes.artist.value;
    }

    if (exists(oPhoto.attributes.artist_link)) {
        this.__artist_link=oPhoto.attributes.artist_link.value;
    }

    if (exists(oPhoto.attributes.courtesy)) {
        this.__courtesy=oPhoto.attributes.courtesy.value;
    }

    if (exists(oPhoto.attributes.courtesy_link)) {
        this.__courtesy_link=oPhoto.attributes.courtesy_link.value;
    }

    if (exists(oPhoto.attributes.year)) {
        this.__year=oPhoto.attributes.year.value;
    }

    if (exists(oPhoto.attributes.location)) {
        this.__location=oPhoto.attributes.location.value;
    }

    if (exists(oPhoto.attributes.copyright)) {
        this.__copyright=oPhoto.attributes.copyright.value;
    }

    /*
        Get functions
     */
    this.gl = function() {
        return this.__location;
    }

    /*
        Draw Image
     */
    this.dp = function(node) {
        this.photo = document.createElement("img");
        this.photo.src = this.__src;
        this.photo.alt = this.__caption;

        node.appendChild(this.photo);

        if(!isEmpty(this.__caption) || !isEmpty(this.__artist))
        {
            this.caption = document.createElement("div");
            this.caption.className = "photoCaption";

            output = "";

            if(!isEmpty(this.__caption)) { output = output + "<i>" + this.__caption + "</i>"; }

            if(!isEmpty(this.__artist) || !isEmpty(this.__courtesy)) {
                if(isEmpty(this.__copyright) || this.copyright) {
                    output += " &copy; ";
                }
                else {
                    output += ". ";
                }

                if(!isEmpty(this.__artist)) {
                    if(!isEmpty(this.__artist_link)) { output = output + "<a href=\"" + this.__artist_link + "\">"; }
                    output += this.__artist;
                    if(!isEmpty(this.__artist_link)) { output = output + "</a>. "; }
                }

                if(!isEmpty(this.__courtesy)) {
                    if(!isEmpty(this.__courtesy_link)) { output = output + "<a href=\"" + this.__courtesy_link + "\">"; }
                    output += this.__courtesy;
                    if(!isEmpty(this.__courtesy_link)) { output = output + "</a>. "; }
                }

                if(!isEmpty(this.__year)) {
                   output = output + this.__year + ".";
                }
            }

            this.caption.innerHTML = output;

            node.appendChild(this.caption);
        }
    }

    /*
        Draw in Place
     */
    this.dip = function() {
        this.dp(this.__node);
    }

    /*
        Draw Credit
     */
    this.dc = function(node) {
        if(!isEmpty(this.__artist))
        {
            this.tag = document.createElement("p");

            this.output = "<b>Image: </b>";

            //if(!isEmpty(this.__link)) { this.output = this.output + "<a href=\"" + this.__link + "\">"; }

            if(!isEmpty(this.__caption)) { this.output = this.output + "<i>" + this.__caption + "</i>"; }

            if(!isEmpty(this.__artist) || !isEmpty(this.__courtesy)) {
                if(isEmpty(this.__copyright) || this.copyright) {
                    this.output += "&copy; ";
                }
                else {
                    this.output += ". ";
                }

                if(!isEmpty(this.__artist)) {
                    this.output += this.__artist;
                    if(!isEmpty(this.__artist_link)) { this.output = this.output + " (<a href=\"" + this.__artist_link + "\">" + this.__artist_link + "</a>)"; }
                    this.output += ". ";
                }

                if(!isEmpty(this.__courtesy)) {
                    this.output += this.__courtesy;
                    if(!isEmpty(this.__courtesy_link)) { this.output = this.output + " (<a href=\"" + this.__courtesy_link + "\">" + this.__courtesy_link + "</a>)"; }
                    this.output += ". ";
                }

                if(!isEmpty(this.__year)) {
                   this.output = this.output + this.__year + ".";
                }
            }

            //if(!isEmpty(this.__link)) { this.output = this.output + "&#160; " + this.__link + "</a>"; }

            this.output += "<br/>";

            this.tag.innerHTML = this.output;

            node.appendChild(this.tag);
        }
    }

    if(this.__location!=='header') {
        this.dip();
    }

    var self=this;
}

/*
    Aj_Photo_Manager
    Manages all images
 */
function Aj_Photo_Manager(){
    this.__photos = new Array();   // Create storage for Aj_Author

    /*
        adds an image to storage
     */
    this.ad = function(oPhoto) {
        this.__photos.push(new Aj_Photo((oPhoto)));
    }

    this.dh = function(oNode) {
        for(var i=0; i<this.__photos.length; i++) {
            if(this.__photos[i].gl()=="header") {
                this.__photos[i].dp(oNode);
            }
        }
    }

    this.dc = function(oNode) {
        // iterate photos
        for (var i=0; i<this.__photos.length; i++) {
            this.__photos[i].dc(oNode);
        }
    }

    var self=this;

}

/*
    BOOK STUFF =====================================
 */

function XMLDoc(){
	if(window.XMLHttpRequest){
    	this.xmlhttp = new XMLHttpRequest();
    }
    else {
    	this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	this.load = function(url, caller, cfunc){
	    this.xmlhttp.onreadystatechange = function() {
	    	cfunc(caller);
	    };
	    this.xmlhttp.open("GET", url, true);
    	this.xmlhttp.send();
	}
	
	var self=this;
}
//var XMLLoader = new XMLDoc();

var Month=new Array(12);
Month[0]="January";
Month[1]="February";
Month[2]="March";
Month[3]="April";
Month[4]="May";
Month[5]="June";
Month[6]="July";
Month[7]="August";
Month[8]="September";
Month[9]="October";
Month[10]="November";
Month[11]="December";

String.prototype.capitalize = function(){
	return this.replace(/(^|\s)([a-z])/g, function(m,p1,p2){ return p1+p2.toUpperCase(); });
};

/*
    Aj_Book
    Variable: oImage - the image node passed in by the manager
 */
function Aj_Book(oBook){
    this.__node = oBook;
    this.__authors = new Array();
    //this.dbiNode = document.createElement("div");
    this.XMLLoader = new XMLDoc();
    
    this.set_all = function(caller) {
    	if(caller.XMLLoader.xmlhttp.readyState==4 && caller.XMLLoader.xmlhttp.status==200) {
		    //document.getElementById("ajax_test").innerHTML=XMLLoader.xmlhttp.responseText;
			x = caller.XMLLoader.xmlhttp.responseXML.getElementsByTagName("Item")[0];
			y = x.getElementsByTagName('ItemAttributes')[0];
	    	//this.__author = x.ItemAttributes.Author.value;
	    	//caller.dbiNode.innerHTML = "George";
	    	//alert(x.getElementsByTagName('ItemAttributes')[0].getElementsByTagName('Author')[0].childNodes[0].nodeValue);
	    	if(isEmpty(caller.__authors[0])){
	    		a = y.getElementsByTagName('Author');
	    		for(var i=0; i<a.length; i++){
	    			if(!isEmpty(a[i].childNodes[0].nodeValue)){
	    				caller.__authors.push(a[i].childNodes[0].nodeValue.toLowerCase().capitalize().replace(/[,\.]/, ''));
	    			}
	    		}
	    		
	    		c = y.getElementsByTagName('Creator');
	    		for(var i=0; i<c.length; i++){
	    			if(!isEmpty(c[i].childNodes[0].nodeValue)){
	    				caller.__authors.push(c[i].childNodes[0].nodeValue.toLowerCase().capitalize().replace(/[,\.]/, '')+" ("+c[i].attributes.Role.value.toLowerCase()+")");
	    			}
	    		}
	    	}
	    	
	    	if(isEmpty(caller.__title) &&
	    		!isEmpty(y.getElementsByTagName('Title')[0].childNodes[0].nodeValue))
	    		{
	    			caller.__title = y.getElementsByTagName('Title')[0].childNodes[0].nodeValue.replace(/[,\.]+$/, '');
	    		}
	    		
	    	if(isEmpty(caller.__publisher) &&
	    		!isEmpty(y.getElementsByTagName('Publisher')[0].childNodes[0].nodeValue))
	    		{
	    			caller.__publisher = y.getElementsByTagName('Publisher')[0].childNodes[0].nodeValue.replace(/[,\.]+$/, '');
	    		}
	    		
	    	if(isEmpty(caller.__pages) &&
	    		!isEmpty(y.getElementsByTagName('NumberOfPages')[0].childNodes[0].nodeValue))
	    		{
	    			caller.__pages = y.getElementsByTagName('NumberOfPages')[0].childNodes[0].nodeValue;
	    		}
	    		
	    	if(isEmpty(caller.__date) &&
	    		!isEmpty(y.getElementsByTagName('PublicationDate')[0].childNodes[0].nodeValue))
	    		{
	    			z = y.getElementsByTagName('PublicationDate')[0].childNodes[0].nodeValue.split('-');
	    			q="";
	    			if(!isEmpty(z[1])) { q = Month[z[1]-1]+" "; }
	    			if(!isEmpty(z[0])) { q += z[0] }
	    			
	    			caller.__date = q;
	    		}
	    		
	    	if(isEmpty(caller.__asin) &&
	    		!isEmpty(x.getElementsByTagName('ASIN')[0].childNodes[0].nodeValue))
	    		{
	    			caller.__asin = x.getElementsByTagName('ASIN')[0].childNodes[0].nodeValue;
	    		}
	    		
	    	if(isEmpty(caller.__ean) &&
	    		!isEmpty(y.getElementsByTagName('EAN')[0].childNodes[0].nodeValue))
	    		{
	    			caller.__ean = y.getElementsByTagName('EAN')[0].childNodes[0].nodeValue;
	    		}
	    		
	    	if(isEmpty(caller.__isbn) &&
	    		!isEmpty(y.getElementsByTagName('ISBN')[0].childNodes[0].nodeValue))
	    		{
	    			caller.__isbn = y.getElementsByTagName('ISBN')[0].childNodes[0].nodeValue;
	    		}
	    		
	    	if(isEmpty(caller.__sku) &&
	    		!isEmpty(y.getElementsByTagName('SKU')[0].childNodes[0].nodeValue))
	    		{
	    			caller.__sku = y.getElementsByTagName('SKU')[0].childNodes[0].nodeValue;
	    		}
	    		
	    	/*if(isEmpty(caller.__dewey) &&
	    		(exists(y.getElementsByTagName('DeweyDecimalNumber')[0].childNodes[0].nodeValue) &&
	    		!isEmpty(y.getElementsByTagName('DeweyDecimalNumber')[0].childNodes[0].nodeValue)))
	    		{
	    			//caller.__dewey = y.getElementsByTagName('DeweyDecimalNumber')[0].childNodes[0].nodeValue;
	    		}*/
	    		
	    	if(isEmpty(caller.__binding) &&
	    		!isEmpty(y.getElementsByTagName('Binding')[0].childNodes[0].nodeValue))
	    		{
	    			caller.__binding = y.getElementsByTagName('Binding')[0].childNodes[0].nodeValue;
	    		}
	    		
	    	if(isEmpty(caller.__img) &&
	    		!isEmpty(x.getElementsByTagName('LargeImage')[0].getElementsByTagName('URL')[0].childNodes[0].nodeValue))
	    		{
	    			caller.__img = x.getElementsByTagName('LargeImage')[0].getElementsByTagName('URL')[0].childNodes[0].nodeValue;
	    		}
	    	caller.dbi2();
	    	caller.dbip2();
	    	caller.dpb2();
	    }
    }

    this.get_amazon_info = function(id) {
    	this.XMLLoader.load("amazon.php?asin="+id, this, this.set_all);
    }
    
    /*
        Get attributes from author tag.
     */
    if (exists(oBook.attributes.asin)) {
        this.__asin=oBook.attributes.asin.value;
    }

    if (exists(oBook.attributes.isbn)) {
        this.__isbn=oBook.attributes.isbn.value;
    }

    if (exists(oBook.attributes.ean)) {
        this.__ean=oBook.attributes.ean.value;
    }

    if (exists(oBook.attributes.title)) {
        this.__title=oBook.attributes.title.value;
    }

    if (exists(oBook.attributes.author)) {
        this.__author=oBook.attributes.author.value;
        this.__authors.push(oBook.attributes.author.value);
    }

    if (exists(oBook.attributes.publisher)) {
        this.__publisher=oBook.attributes.publisher.value;
    }

    if (exists(oBook.attributes.date)) {
        this.__date=oBook.attributes.date.value;
    }

    if (exists(oBook.attributes.pages)) {
        this.__pages=oBook.attributes.pages.value;
    }

    if (exists(oBook.attributes.img)) {
        this.__img=oBook.attributes.img.value;
    }

    if (exists(oBook.attributes.show_credit)) {
        this.__show_credit=oBook.attributes.show_credit.value;
    }

    if (exists(oBook.attributes.in_passing)) {
        this.__in_passing=oBook.attributes.in_passing.value;
    }
    
    if(!isEmpty(this.__asin)) {
        this.get_amazon_info(this.__asin);
    }
    else if(!isEmpty(this.__isbn)) {
        this.get_amazon_info(this.__isbn);
    }

    /*
        Draw functions
     */
    this.get_purchase_info = function() {
        this.output = document.createElement("div");
        this.output.className = "purchase_info";

        this.output1 = document.createElement("p");
        this.output1.className = "purchase_book_title";
        this.output1.innerText = this.__title;

        this.output2 = document.createElement("p");
        this.output2.className = "purchase_book_author";
        this.output2.innerText = "";
        
        for (var i=0; i<this.__authors.length; i++) {
            if(i>0) {                   // determine punctuation to be used
                if((this.__authors.length-i)<2) { this.output2.innerText += " and "; }
                else { this.output2.innerText += ", "; }
            }
        	// get author name from Aj_Author
    	    this.output2.innerText += this.__authors[i];
	    }
        
        //this.output2.innerText = this.__authors[0];

        this.output.appendChild(this.output1);
        this.output.appendChild(this.output2);

        return this.output;
    }

    this.get_purchase_img = function() {
        this.img = document.createElement("img");

        if(!isEmpty(this.__img))
        {
            this.img.src = this.__img;
        }
        else
        {
            this.img.src = aj_settings.img_am(this.__isbn);
        }

        this.output = document.createElement("div");
        this.output.className = "purchase_img";
        this.output.appendChild(this.img);

        return this.output;
    }

    this.get_purchase_links = function() {
        this.output = document.createElement("div");
        this.output.className = "purchase_links";

        this.ulist = document.createElement("ul");

        this.li1 = document.createElement("li");
        this.li1.innerHTML = aj_settings.li_in(this.__isbn);
        this.li2 = document.createElement("li");
        this.li2.innerHTML = aj_settings.li_po(this.__isbn);
        this.li3 = document.createElement("li");
        this.li3.innerHTML = aj_settings.li_am(this.__isbn);
        this.li4 = document.createElement("li");
        this.li4.innerHTML = aj_settings.li_bn(this.__isbn);

        this.ulist.appendChild(this.li1);
        this.ulist.appendChild(this.li2);
        this.ulist.appendChild(this.li3);
        this.ulist.appendChild(this.li4);

        this.output.appendChild(this.ulist);

        return this.output;
    }

    this.dbi2 = function() {
        if(!isEmpty(this.__show_credit) && this.__show_credit){
            output = "<b>";
            
            for (var i=0; i<this.__authors.length; i++) {
            	if(i>0) {                   // determine punctuation to be used
        	        if((this.__authors.length-i)<2) { output += " and "; }
    	            else { output += ", "; }
	            }
        	    // get author name from Aj_Author
    	        output += this.__authors[i];
	        }
        
            //output = output + this.__authors[0];
        
        	output = output + "<br/><i>" + this.__title + "</i></b><br/>" + this.__publisher + ", " +
                this.__date + ". " + this.__pages + " pp.<br/><br/>";

			x = document.getElementById("dbi_"+this.__isbn);
			x.innerHTML += output;
        }
    }

	this.dbi = function(oNode) {
		x = document.createElement("div");
		x.id = "dbi_"+this.__isbn;
		oNode.appendChild(x);
    }

    this.dbip2 = function() {
        if(!isEmpty(this.__in_passing) && this.__in_passing){
            this.output = "<b>";
            
            for (var i=0; i<this.__authors.length; i++) {
            	if(i>0) {                   // determine punctuation to be used
        	        if((this.__authors.length-i)<2) { this.output += " and "; }
    	            else { this.output += ", "; }
	            }
        	    // get author name from Aj_Author
    	        this.output += this.__authors[i];
	        }
	        
	        this.output = this.output + ", <i>" + this.__title + "</i></b> (" + this.__publisher + ", " +
                this.__date + "); ";

			x = document.getElementById("dbip_"+this.__isbn);
            x.innerHTML += this.output;
        }
    }

    this.dbip = function(oNode) {
        x = document.createElement("span");
        x.id = "dbip_"+this.__isbn;
        oNode.appendChild(x);
    }

    this.dpb2 = function() {
        if(!isEmpty(this.__isbn)) {
        	x = document.getElementById("dpb_"+this.__isbn);
        	
            x.appendChild(this.get_purchase_info());
            x.appendChild(this.get_purchase_links());
            x.appendChild(this.get_purchase_img());

            this.output = document.createElement("div");
            this.output.className = "clearance";
            x.appendChild(this.output);
        }
    }

    this.dpb = function(oNode) {
        x = document.createElement("div");
        x.id = "dpb_"+this.__isbn;
        oNode.appendChild(x);
    }

    var self=this;
}

/*
    Aj_Book_Manager
    Manages all images
 */
function Aj_Book_Manager(){
    this.__books = new Array();   // Create storage for Aj_Author

    /*
        adds an image to storage
     */
    this.ad = function(oBook) {
        this.__books.push(new Aj_Book((oBook)));
    }

    this.dbi = function(oNode) {
        for(var i=0; i<this.__books.length; i++) {
            this.__books[i].dbi(oNode);
        }

        is_first = true;
        for(var i=0; i<this.__books.length; i++) {
            if(is_first){
                oNode.innerHTML += "<p><b>Books mentioned in passing:</b></p>";
                is_first = false;
            }
            this.__books[i].dbip(oNode);
        }
        oNode.innerHTML += "<br/><br/>";
    }

    this.dpb = function(oNode) {
        if(this.__books.length > 0){
            this.pretext = "Buying books by clicking on the icons below helps support the <i>Los Angeles Review of Books.</i>  " +
                "Any item you buy after clicking through pays us a royalty, which we in turn use to pay our bills.  " +
                "If you would like to make a more substantial gift, please hit the " +
                "<a href=\"http://lareviewofbooks.org/donate\" target=\"_blank\">DONATE button here</a>, above, or below.<br/><br/>";
            oNode.innerHTML += this.pretext;

            // iterate books
            for (var i=0; i<this.__books.length; i++) {
                this.__books[i].dpb(oNode);
            }
        }
    }

    var self=this;

}

/*
    SUBJECT STUFF =====================================
 */

/*
    Aj_Subject
    Variable: oImage - the image node passed in by the manager
 */
function Aj_Subject(){
    /*
        Get attributes from author tag.
     */
    this.set = function(oSubject) {
        if (exists(oSubject.attributes.text)) {
            this.__text=oSubject.attributes.text.value;
        }
    }

    /*
        Draw functions
     */
    this.d = function(oNode) {
        if(!isEmpty(this.__text)){
            this.output = document.createElement("div");
            this.output.className = "article_subject";
            this.output.innerHTML = this.__text;

            oNode.appendChild(this.output);
        }
    }

    var self=this;
}

/*
    NAMEDROP STUFF =====================================
 */

/*
    Aj_NameDrop
    Variable: oImage - the image node passed in by the manager
 */
function Aj_NameDrop(oNameDrop){
    this.__node = oNameDrop;
    /*
        Get attributes from author tag.
     */
    if (exists(oNameDrop.attributes.name)) {
        this.__name=oNameDrop.attributes.name.value;
    }
    else {
        this.__name=oNameDrop.innerHTML;
    }

    /*
        Draw functions
     */
    this.d = function(oNode) {
        this.output = document.createElement("span");
        this.output.className = "aj_namedrop";
        this.output.innerHTML = this.__name;

        oNode.appendChild(this.output);
    }

    var self=this;
}

/*
    Aj_NameDrop_Manager
    Manages all images
 */
function Aj_NameDrop_Manager(){
    this.__namedrops = new Array();   // Create storage for Aj_Author

    /*
        adds an image to storage
     */
    this.ad = function(oNameDrop) {
        this.__namedrops.push(new Aj_NameDrop((oNameDrop)));
    }

    this.dnd = function(oNode) {
        if(this.__namedrops.length>0) {
            this.output = document.createElement("div");    // create div
            this.output.className = "aj_namedrops";                       // div class = aj_byline

            this.output.innerHTML += "Names dropped: ";
            
            // iterate authors
            for (var i=0; i<this.__namedrops.length; i++) {
                if(i>0) {                   // determine punctuation to be used
                    if((this.__namedrops.length-i)<2) { this.output.innerHTML += " and "; }
                    else { this.output.innerHTML += ", "; }
                }
                // get author name from Aj_Author
                this.__namedrops[i].d(this.output);
            }

            oNode.appendChild(this.output);     // add all this to the page.
        }
    }

    var self=this;
}

/*
    instantiate managers ================================
 */
var aj_authors = new Aj_Author_Manager();
var aj_photos = new Aj_Photo_Manager();
var aj_books = new Aj_Book_Manager();
var aj_subject = new Aj_Subject();
var aj_namedrops = new Aj_NameDrop_Manager();

/*
    Get all node to search for custom nodes ================
 */
var arr = document.getElementsByTagName("*");

// iterate
for(var i=0; i<arr.length; i++) {
    var tgNm = arr.item(i).nodeName.toLowerCase();      // lowercase the tag

    // Tag: aj:settings
    if (tgNm==st) {
        var aj_settings = new Settings(arr.item(i));
    }
    // Tag: aj:header
    else if (tgNm==aht) {
        var aj_header = new AJHM(arr.item(i));
    }
    // Tag: aj:footer
    else if(tgNm==aft) {
        var aj_footer = new AJFM(arr.item(i));
    }
    // Tag: aj:author
    else if(tgNm==at) {
        aj_authors.ad(arr.item(i));
    }
    else if(tgNm==pt) {
        aj_photos.ad(arr.item(i));
    }
    else if(tgNm==bt) {
        aj_books.ad(arr.item(i));
    }
    else if(tgNm==sjt) {
        aj_subject.set(arr.item(i));
    }
    else if(tgNm==ndt) {
        aj_namedrops.ad(arr.item(i));
    }
}

/*
    Draw everything =====================================
 */
addStylesheet("aj_blog_styles.css");
aj_header.d();
aj_footer.d();



function isEmpty(test) {
    return (test == null || test == "");
}

function exists(test) {
    return ((typeof test != 'undefined') &&
            test !== null);
}

function addStylesheet(sheet) {
    var styleElement = document.createElement("link");
    styleElement.rel = "stylesheet";
    styleElement.type = "text/css";
    styleElement.href = sheet;
    document.getElementsByTagName("head")[0].appendChild(styleElement);
}

function addCss(cssCode) {
    var styleElement = document.createElement("style");
    styleElement.type = "text/css";
    if(styleElement.styleSheet) {
        styleElement.styleSheet.cssText = cssCode;
    }
    else {
        styleElement.appendChild(document.createTextNode(cssCode));
    }
    document.getElementsByTagName("head")[0].appendChild(styleElement);
}




/*function jax_test(){
    if(window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    }
    else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("GET", "amazon.php?asin=1439149038", false);
    xmlhttp.send();
    document.getElementById("ajax_test").innerHTML=xmlhttp.responseText;
}*/


/*function ajax_test_responder() {
	document.getElementById("ajax_test").innerHTML=xmlhttp.responseText;
}

function ajax_test(url, cfunc){
    if(window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    }
    else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

	xmlhttp.onreadystatechange=cfunc;
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}


var id="1439149038";
ajax_test("amazon.php?asin="+id, ajax_test_responder);*/

/*function ajax_tester(){
	if(window.XMLHttpRequest){
    	this.xmlhttp = new XMLHttpRequest();
    }
    else {
    	this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}

	this.ajax_test = function(url, cfunc){
	    this.xmlhttp.onreadystatechange=cfunc;
	    this.xmlhttp.open("GET", url, true);
    	this.xmlhttp.send();
	}
	
	var self=this;
}
var ajt = new ajax_tester();

function ajax_test_responder() {
	this.respond = function(){
		if(ajt.xmlhttp.readyState==4 && ajt.xmlhttp.status==200) {
            //this.set_all(xmlhttp.responseXML);
            //this.__dbiNode.innerHTML = "Bob";
            document.getElementById("ajax_test").innerHTML=ajt.xmlhttp.responseText;
        }
	}
}
var atr = new ajax_test_responder();

var id="1416552960";
ajt.ajax_test("amazon.php?asin="+id, atr.respond);*/



/*var xmlhttp;
function loadXMLDoc(url,cfunc) {
    if(window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    }
    else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=cfunc;
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}*/

/*function ajax_test() {
    if(window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    }
    else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=cfunc;
    xmlhttp.open("GET","amazon.php?asin=1439149038",true);
    xmlhttp.send();
}*/